<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>canvas绘制柱状图 | East'blog</title><meta name="author" content="侯玉东"><meta name="copyright" content="侯玉东"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="canvas绘制柱状图">
<meta property="og:type" content="article">
<meta property="og:title" content="canvas绘制柱状图">
<meta property="og:url" content="https://houyudong1989.gitee.io/blog/2023/12/02/harmonyOS/Canvas%E7%BB%98%E5%88%B6%E6%9F%B1%E7%8A%B6%E5%9B%BE/index.html">
<meta property="og:site_name" content="East&#39;blog">
<meta property="og:description" content="canvas绘制柱状图">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://houyudong1989.gitee.io/blog/img/library1.jpeg">
<meta property="article:published_time" content="2023-12-02T02:04:45.529Z">
<meta property="article:modified_time" content="2024-04-17T01:21:30.066Z">
<meta property="article:author" content="侯玉东">
<meta property="article:tag" content="harmonyOS">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://houyudong1989.gitee.io/blog/img/library1.jpeg"><link rel="shortcut icon" href="/blog/img/head.jpg"><link rel="canonical" href="https://houyudong1989.gitee.io/blog/2023/12/02/harmonyOS/Canvas%E7%BB%98%E5%88%B6%E6%9F%B1%E7%8A%B6%E5%9B%BE/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/blog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'canvas绘制柱状图',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-04-17 09:21:30'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>(()=>{
  const $loadingBox = document.getElementById('loading-box')
  const $body = document.body
  const preloader = {
    endLoading: () => {
      $body.style.overflow = ''
      $loadingBox.classList.add('loaded')
    },
    initLoading: () => {
      $body.style.overflow = 'hidden'
      $loadingBox.classList.remove('loaded')
    }
  }

  preloader.initLoading()
  window.addEventListener('load',() => { preloader.endLoading() })

  if (true) {
    document.addEventListener('pjax:send', () => { preloader.initLoading() })
    document.addEventListener('pjax:complete', () => { preloader.endLoading() })
  }
})()</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/blog/img/head.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">70</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">26</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友联</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg fixed" id="page-header" style="background-image: url('/blog/img/library4.jpeg')"><nav id="nav"><span id="blog-info"><a href="/blog/" title="East'blog"><img class="site-icon" src="/blog/img/head.jpg"/><span class="site-name">East'blog</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友联</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">canvas绘制柱状图</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-12-02T02:04:45.529Z" title="发表于 2023-12-02 10:04:45">2023-12-02</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-04-17T01:21:30.066Z" title="更新于 2024-04-17 09:21:30">2024-04-17</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/harmonyOS/">harmonyOS</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="canvas绘制柱状图"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">最近在做新功能需求的时候，需要丛一个柱状图，但目前由于鸿蒙生态不是特别丰富，所以准备使用canvas自己手撸一个柱状图。顺便研究一下canvas的使用。canvas是一个画布，提供给我们一个用于绘制图像的地方，它本身不提供各种绘制方法，需要使用CanvasRenderingContext2D对象和OffscreenCanvasRenderingContext2D对象进行绘制，就像我们画画一样，我们需要用笔在纸上进行绘制，canvas就是我们的纸，而CanvasRenderingContext2D对象和OffscreenCanvasRenderingContext2D对象就是我们画画的笔。</span><br></pre></td></tr></table></figure>

<h1 id="Canvas"><a href="#Canvas" class="headerlink" title="Canvas"></a>Canvas</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Canvas提供画布组件，用于自定义绘制图形，开发者使用CanvasRenderingContext2D对象和OffscreenCanvasRenderingContext2D对象在Canvas组件上进行绘制，绘制对象可以是基础形状、文本、图片等。</span><br></pre></td></tr></table></figure>

<h1 id="CanvasRenderingContext2D-和-OffscreenCanvasRenderingContext2D"><a href="#CanvasRenderingContext2D-和-OffscreenCanvasRenderingContext2D" class="headerlink" title="CanvasRenderingContext2D 和 OffscreenCanvasRenderingContext2D"></a>CanvasRenderingContext2D 和 OffscreenCanvasRenderingContext2D</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">两个物件在某种意义上来讲其实是一样的，都是用于绘制图形的画笔，区别在于OffscreenCanvasRenderingContext2D是离屏绘制，它会将需要绘制的内容先绘制在缓存区，然后将其转换成图片一次性绘制到canvas中，加快了绘制速度，而CanvasRenderingContext2D是直接在canvas上进行绘制。两者在使用方式，调用函数上是等价的。</span><br></pre></td></tr></table></figure>

<h1 id="使用CanvasRenderingContext2D进行绘制"><a href="#使用CanvasRenderingContext2D进行绘制" class="headerlink" title="使用CanvasRenderingContext2D进行绘制"></a>使用CanvasRenderingContext2D进行绘制</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">10,20,100,3001、创建CanvasRenderingContext2D设置对象，进行初始化设置，如是否开启抗锯齿</span><br><span class="line">	private settings: RenderingContextSettings = new RenderingContextSettings(true)</span><br><span class="line">2、创建CanvasRenderingContext2D对象</span><br><span class="line">	private paint: CanvasRenderingContext2D= new CanvasRenderingContext2D(this.settings)</span><br><span class="line">3、创建canvas，将paint传入canvas，在onReady函数中进行绘制</span><br><span class="line">	Canvas(this.paint).width(&#x27;100%&#x27;).height(&#x27;50%&#x27;).backgroundColor(0xf5dc62).onReady(() =&gt; &#123;</span><br><span class="line">        this.paint.strokeRect(10,20,100,300)</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">4、在canvas的onReady函数中进行调用绘制图像，如绘制一个矩形</span><br><span class="line">	this.paint.strokeRect(10,20,100,300);</span><br></pre></td></tr></table></figure>

<h1 id="使用OffscreenCanvasRenderingContext2D进行绘制"><a href="#使用OffscreenCanvasRenderingContext2D进行绘制" class="headerlink" title="使用OffscreenCanvasRenderingContext2D进行绘制"></a>使用OffscreenCanvasRenderingContext2D进行绘制</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">1、创建CanvasRenderingContext2D设置对象，进行初始化设置，如是否开启抗锯齿</span><br><span class="line">	private settings: RenderingContextSettings = new RenderingContextSettings(true)</span><br><span class="line">2、创建CanvasRenderingContext2D对象</span><br><span class="line">	private paint: CanvasRenderingContext2D= new CanvasRenderingContext2D(this.settings)</span><br><span class="line">3、创建OffscreenCanvasRenderingContext2D对象，设置画布的宽度，高度和设置对象</span><br><span class="line">	private offPaint: OffscreenCanvasRenderingContext2D = new OffscreenCanvasRenderingContext2D(600, 600, this.settings)</span><br><span class="line">4、创建canvas，将paint传入canvas，在onReady函数中进行绘制</span><br><span class="line">	Canvas(this.paint).width(&#x27;100%&#x27;).height(&#x27;50%&#x27;).backgroundColor(0xf5dc62).onReady(() =&gt; &#123;</span><br><span class="line">        this.paint.strokeRect(10,20,100,300)</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">5、在canvas的onReady函数中进行调用绘制图像，如绘制一个矩形</span><br><span class="line">	   this.offPaint.strokeRect(10,20,100,300);</span><br><span class="line">6、将离屏缓存图像渲染到画布上进行显示</span><br><span class="line">	 let image = this.offPaint.transferToImageBitmap();</span><br><span class="line">          this.paint.transferFromImageBitmap(image);</span><br></pre></td></tr></table></figure>

<h1 id="常用函数说明"><a href="#常用函数说明" class="headerlink" title="常用函数说明"></a>常用函数说明</h1><blockquote>
<p>关于坐标说明</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">在canvas进行绘制时，所有的坐标都是基于画布的位置开始进行计算。画布的起始位置就是坐标的起始位置。</span><br><span class="line">如下图所示，我们通过绘制填充矩形基于画布的坐标进行绘制。代码如下：</span><br><span class="line"> Canvas(this.paint).width(&#x27;80%&#x27;).height(&#x27;50%&#x27;).margin(30).backgroundColor(0xf5dc62).onReady(() =&gt; &#123;</span><br><span class="line">        this.paint.fillStyle = &#x27;#0097D4&#x27;;</span><br><span class="line">        this.paint.fillRect(15,15,40,40)</span><br><span class="line">      &#125;)</span><br></pre></td></tr></table></figure>

<p><img src="1701655194443.png" alt="1701655194443"></p>
<blockquote>
<p>this.paint.width</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">当创建好CanvasRenderingContext2D对象并传入canvas以后，可以通过该函数获取画布的宽度</span><br></pre></td></tr></table></figure>

<blockquote>
<p>this.paint.height</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">当创建好CanvasRenderingContext2D对象并传入canvas以后，可以通过该函数获取画布的高度</span><br></pre></td></tr></table></figure>

<blockquote>
<p>fill…</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">fill开头的函数指代对封闭路径进行填充。也可以单独定义path对象最后调用fill进行填充绘制</span><br><span class="line">如：</span><br><span class="line">this.paint.fillStyle = &#x27;#0097D4&#x27;;</span><br><span class="line">this.paint.fillRect(55,10,40,40)</span><br><span class="line">this.paint.fillText(&quot;Hello&quot;,55,70)</span><br></pre></td></tr></table></figure>

<p><img src="https://file+.vscode-resource.vscode-cdn.net/Users/houyudong/Documents/Typora/blog/source/_posts/harmonyOS/Canvas%E7%BB%98%E5%88%B6%E6%9F%B1%E7%8A%B6%E5%9B%BE/1701656316804.png" alt="1701656316804"></p>
<blockquote>
<p>stroke…</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">对目标进行描边操作，也可以单独定义path对象最后调用stroke进行描边绘制</span><br><span class="line">如：</span><br><span class="line">this.paint.strokeStyle=&quot;#0033dd&quot;</span><br><span class="line">this.paint.strokeRect(10,10,40,40)</span><br><span class="line">this.paint.font =&#x27;50px sans-serif&#x27;;</span><br><span class="line">this.paint.strokeText(&quot;Hello&quot;,10,70)</span><br></pre></td></tr></table></figure>

<p><img src="https://file+.vscode-resource.vscode-cdn.net/Users/houyudong/Documents/Typora/blog/source/_posts/harmonyOS/Canvas%E7%BB%98%E5%88%B6%E6%9F%B1%E7%8A%B6%E5%9B%BE/1701656331439.png" alt="1701656331439"></p>
<blockquote>
<p>globalAlpha</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">设置对象的透明度</span><br><span class="line">   this.paint.globalAlpha=0.4</span><br><span class="line">   this.paint.fillStyle = &#x27;#0097D4&#x27;;</span><br><span class="line">   this.paint.fillRect(5,10,40,40)</span><br><span class="line">   this.paint.globalAlpha=1.0</span><br><span class="line">   this.paint.fillStyle = &#x27;#0097D4&#x27;;</span><br><span class="line">   this.paint.fillRect(55,10,40,40)</span><br></pre></td></tr></table></figure>

<p><img src="1701658403699.png" alt="1701658403699"></p>
<blockquote>
<p>绘制线条</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">在canvas中，我们可以绘制多个图形，每次绘制新的图形需要调用beginPath开始一个新的绘制路径，通过fill或者stroke函数将原来的路径进行绘制。beginPath和stroke/fill标志着一个图形的起止位置。</span><br><span class="line">    this.paint.lineWidth =10</span><br><span class="line">    this.paint.beginPath()</span><br><span class="line">        // &quot;butt&quot; | &quot;round&quot; | &quot;square&quot;</span><br><span class="line">    this.paint.lineCap = &#x27;butt&#x27;</span><br><span class="line">    this.paint.moveTo(10,this.paint.height)</span><br><span class="line">    this.paint.lineTo(10, this.paint.height-150)</span><br><span class="line">    this.paint.stroke()</span><br></pre></td></tr></table></figure>

<p><img src="1701659924140.png" alt="1701659924140"></p>
<h1 id="CanvasGradient-颜色渐变"><a href="#CanvasGradient-颜色渐变" class="headerlink" title="CanvasGradient 颜色渐变"></a>CanvasGradient 颜色渐变</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">//创建线性颜色渐变对象，设置渐变的起始和结束位置</span><br><span class="line">        let color = this.paint.createLinearGradient(10,this.paint.height, 10, this.paint.height-150)</span><br><span class="line">//设置渐变颜色值</span><br><span class="line">        color.addColorStop(0.0,&quot;#9BE15D&quot;)</span><br><span class="line">        color.addColorStop(1.0,&quot;#00E3AE&quot;)</span><br><span class="line">//开始绘制线条，由于绘制线条是非封闭区间，所有需要使用strokestyle，使用fillstyle会没有效果</span><br><span class="line">        this.paint.lineWidth =10</span><br><span class="line">        this.paint.beginPath()</span><br><span class="line">        // &quot;butt&quot; | &quot;round&quot; | &quot;square&quot;</span><br><span class="line">        this.paint.lineCap = &#x27;round&#x27;</span><br><span class="line">        this.paint.strokeStyle=color</span><br><span class="line">        this.paint.moveTo(10,this.paint.height)</span><br><span class="line">        this.paint.lineTo(10, this.paint.height-150)</span><br><span class="line">        this.paint.stroke()</span><br></pre></td></tr></table></figure>

<p><img src="1701660897955.png" alt="1701660897955"></p>
<h1 id="绘制柱状图"><a href="#绘制柱状图" class="headerlink" title="绘制柱状图"></a>绘制柱状图</h1><blockquote>
<p>绘制柱状图所需要的技能基本准备完成，现在开始进行绘制我们的柱状图，先上效果图：</p>
</blockquote>
<p><img src="1701661394781.png" alt="1701661394781"></p>
<blockquote>
<p>1、首先绘制柱状图的背景，有效果图我们可以看到北京市纯白色，由灰色边框及阴影构成，同时需要设置圆角。我们可以对画布进行设置边框，阴影等效果.效果如下</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Canvas(this.paint).width(&#x27;90%&#x27;).height(200).margin(30).borderRadius(20).shadow(&#123;radius:40,color:&quot;#e6e6e6&quot;,offsetX:0,offsetY:0&#125;)</span><br></pre></td></tr></table></figure>

<p><img src="1701668505765.png" alt="1701668505765"></p>
<blockquote>
<p>2、背景设置完成以后，我们需要进行绘制目标线，目标线的位置我们设置为固定位，永远在画布的2&#x2F;3处。</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"> <span class="title function_">targetLine</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="comment">// 计算目标线绘制的位置</span></span><br><span class="line">    <span class="keyword">let</span> x = <span class="variable language_">this</span>.<span class="property">marginleft</span></span><br><span class="line">    <span class="keyword">let</span> y = classclassMath.<span class="title function_">ceil</span>(<span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">height</span>/<span class="number">3</span>)-<span class="title function_">vp2px</span>(<span class="number">10</span>)</span><br><span class="line"><span class="comment">//绘制目标文本</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">textBaseline</span>=<span class="string">&quot;middle&quot;</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">textAlign</span>=<span class="string">&quot;center&quot;</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">fillStyle</span>=<span class="string">&quot;#cccccc&quot;</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">font</span> = <span class="string">&#x27;28px sans-serif&#x27;</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">fillText</span>(<span class="string">&quot;目标&quot;</span>,x,y)</span><br><span class="line"><span class="comment">// 绘制目标线</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">beginPath</span>()</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">strokeStyle</span>=<span class="string">&quot;#eeeeee&quot;</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">lineWidth</span> =<span class="number">1</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">beginPath</span>()</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">lineCap</span> = <span class="string">&#x27;round&#x27;</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">moveTo</span>(x+<span class="title function_">vp2px</span>(<span class="number">5</span>),y)</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">lineTo</span>(<span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">width</span>-x+<span class="title function_">vp2px</span>(<span class="number">5</span>)-<span class="variable language_">this</span>.<span class="property">marginRight</span>,y)</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">stroke</span>()</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<p><img src="1701678569801.png" alt="1701678569801"></p>
<blockquote>
<p>绘制图表</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//绘制柱状图</span></span><br><span class="line"><span class="title function_">drawBar</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="keyword">let</span> allWidth = <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">width</span> - <span class="variable language_">this</span>.<span class="property">barMargin</span></span><br><span class="line">  <span class="keyword">let</span> padding = (allWidth-<span class="variable language_">this</span>.<span class="property">barWidth</span>*<span class="variable language_">this</span>.<span class="property">barNum</span>)/(<span class="variable language_">this</span>.<span class="property">barNum</span>-<span class="number">1</span>)</span><br><span class="line">  <span class="keyword">let</span> startX = <span class="variable language_">this</span>.<span class="property">barMargin</span> + padding/<span class="number">2</span> <span class="comment">//第一个bar的x坐标</span></span><br><span class="line">  <span class="keyword">let</span> startY = <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">height</span> - <span class="variable language_">this</span>.<span class="property">marginBottom</span> - <span class="title function_">vp2px</span>(<span class="number">5</span>) <span class="comment">//所有bar的底点位置坐标   画布高度-底边间距-文字高度</span></span><br><span class="line">  <span class="keyword">let</span> allHeight = <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">height</span>-<span class="variable language_">this</span>.<span class="property">marginTop</span> - <span class="variable language_">this</span>.<span class="property">marginBottom</span> - <span class="title function_">vp2px</span>(<span class="number">10</span>)</span><br><span class="line">  <span class="keyword">let</span> unitValue = allHeight/<span class="variable language_">this</span>.<span class="property">targetNum</span></span><br><span class="line">  <span class="comment">// 设置线条渐变颜色</span></span><br><span class="line">  <span class="keyword">let</span> color = <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">createLinearGradient</span>(<span class="number">10</span>, <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">height</span>, <span class="number">10</span>, <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">height</span> - <span class="number">150</span>)</span><br><span class="line">  color.<span class="title function_">addColorStop</span>(<span class="number">0.0</span>, <span class="string">&quot;#9BE15D&quot;</span>)</span><br><span class="line">  color.<span class="title function_">addColorStop</span>(<span class="number">1.0</span>, <span class="string">&quot;#00E3AE&quot;</span>)</span><br><span class="line">  <span class="comment">// 设置线条宽度</span></span><br><span class="line">  <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">lineWidth</span> =<span class="variable language_">this</span>. barWidth</span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">let</span> index = <span class="number">0</span>; index &lt; <span class="variable language_">this</span>.<span class="property">items</span>.<span class="property">length</span>; index++) &#123;</span><br><span class="line">    <span class="comment">// 计算绘制的坐标位置</span></span><br><span class="line">    <span class="keyword">let</span> x = startX + (index * padding)</span><br><span class="line">    <span class="keyword">let</span> y = unitValue *<span class="variable language_">this</span>.<span class="property">items</span>[index].<span class="property">value</span>&gt;allHeight?<span class="attr">allHeight</span>:unitValue *<span class="variable language_">this</span>.<span class="property">items</span>[index].<span class="property">value</span></span><br><span class="line">    <span class="comment">//绘制每一个柱状图</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">beginPath</span>()</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">lineCap</span> = <span class="string">&#x27;round&#x27;</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">strokeStyle</span> = <span class="variable language_">this</span>.<span class="property">items</span>[index].<span class="property">value</span>&lt;<span class="number">100</span>?<span class="string">&quot;#e6e6e6&quot;</span>:color</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">moveTo</span>(x, startY)</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">lineTo</span>(x, startY - y)</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">stroke</span>()</span><br><span class="line">    <span class="comment">//绘制每一个柱状图对应的标签文本</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">beginPath</span>()</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">textBaseline</span> = <span class="string">&quot;middle&quot;</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">textAlign</span> = <span class="string">&quot;center&quot;</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">fillStyle</span> = <span class="string">&quot;#999999&quot;</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">font</span> = <span class="string">&#x27;28px sans-serif&#x27;</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">fillText</span>(<span class="variable language_">this</span>.<span class="property">items</span>[index].<span class="property">lable</span>, x,<span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">height</span> - <span class="variable language_">this</span>.<span class="property">marginBottom</span> )</span><br><span class="line">  <span class="comment">//   绘制每一个柱状图对应的value值</span></span><br><span class="line">    <span class="keyword">if</span>(<span class="variable language_">this</span>.<span class="property">showValue</span>)&#123;</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">beginPath</span>()</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">textBaseline</span> = <span class="string">&quot;middle&quot;</span></span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">textAlign</span> = <span class="string">&quot;center&quot;</span></span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">fillStyle</span> = <span class="string">&quot;#999999&quot;</span></span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">font</span> = <span class="string">&#x27;28px sans-serif&#x27;</span></span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">fillText</span>(<span class="variable language_">this</span>.<span class="property">items</span>[index].<span class="property">value</span>.<span class="title function_">toString</span>(), x, startY - y-<span class="title function_">vp2px</span>(<span class="number">4</span>))</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>奉上最终效果图</p>
</blockquote>
<p><img src="1701678705819.png" alt="1701678705819"></p>
<blockquote>
<p>源代码，只实现了最基本的功能，没有考虑数量过多是否滑动问题，还有点击事件需要监听触摸事件，判断当前点击的x和y轴坐标判断哪个柱状条被点击，然后再做对应处理，我现在没有这个功能需求，就先搁置了。</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">Item</span> &#123;</span><br><span class="line">  <span class="attr">lable</span>:string</span><br><span class="line">  <span class="attr">value</span>:number</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">@<span class="title class_">Component</span></span><br><span class="line"><span class="keyword">export</span> struct <span class="title class_">Bargraph</span> &#123;</span><br><span class="line">  private <span class="attr">settings</span>: <span class="title class_">RenderingContextSettings</span> = <span class="keyword">new</span> <span class="title class_">RenderingContextSettings</span>(<span class="literal">true</span>)</span><br><span class="line">  private <span class="attr">paint</span>: <span class="title class_">CanvasRenderingContext2D</span> = <span class="keyword">new</span> <span class="title class_">CanvasRenderingContext2D</span>(<span class="variable language_">this</span>.<span class="property">settings</span>)</span><br><span class="line">  <span class="comment">// 设置内容在画布上的间距</span></span><br><span class="line">  private <span class="attr">marginTop</span>: number = <span class="title function_">vp2px</span>(<span class="number">2</span>)</span><br><span class="line">  private <span class="attr">marginBottom</span>: number = <span class="title function_">vp2px</span>(<span class="number">7</span>)</span><br><span class="line">  private <span class="attr">marginleft</span>: number = <span class="title function_">vp2px</span>(<span class="number">5</span>)</span><br><span class="line">  private <span class="attr">marginRight</span>: number = <span class="title function_">vp2px</span>(<span class="number">5</span>)</span><br><span class="line">  private <span class="attr">barMargin</span>: number = <span class="title function_">vp2px</span>(<span class="number">10</span>) <span class="comment">//柱状图距离左右两侧的距离</span></span><br><span class="line">  private <span class="attr">showValue</span>: boolean = <span class="literal">true</span> <span class="comment">//设置显示每个柱状图文本值，默认显示最大值，false全部显示</span></span><br><span class="line">  private <span class="attr">barNum</span>: number = <span class="number">7</span> <span class="comment">//设置柱状图数量，默认7个</span></span><br><span class="line">  private <span class="attr">barWidth</span>:number = <span class="number">10</span></span><br><span class="line">  private <span class="attr">targetNum</span>:number =<span class="number">8000</span></span><br><span class="line">  @<span class="title class_">State</span> <span class="attr">items</span>:<span class="title class_">Array</span>&lt;<span class="title class_">Item</span>&gt;=<span class="keyword">new</span> <span class="title class_">Array</span>&lt;<span class="title class_">Item</span>&gt;();</span><br><span class="line"></span><br><span class="line">  <span class="title function_">aboutToAppear</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> index = <span class="number">0</span>; index &lt; <span class="number">7</span>; index++) &#123;</span><br><span class="line">       <span class="keyword">let</span> item = <span class="keyword">new</span> <span class="title class_">Item</span>()</span><br><span class="line">      item.<span class="property">lable</span>=<span class="string">`5/0<span class="subst">$&#123;index+<span class="number">1</span>&#125;</span>`</span></span><br><span class="line">      item.<span class="property">value</span> =  <span class="number">2000</span>*index</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">items</span>.<span class="title function_">push</span>(item)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">//绘制目标线</span></span><br><span class="line">  <span class="title function_">targetLine</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// 计算目标线绘制的位置</span></span><br><span class="line">    <span class="keyword">let</span> x = <span class="variable language_">this</span>.<span class="property">marginleft</span></span><br><span class="line">    <span class="keyword">let</span> y = <span class="title class_">Math</span>.<span class="title function_">ceil</span>(<span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">height</span> / <span class="number">3</span>)</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">textBaseline</span> = <span class="string">&quot;middle&quot;</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">textAlign</span> = <span class="string">&quot;center&quot;</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">fillStyle</span> = <span class="string">&quot;#cccccc&quot;</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">font</span> = <span class="string">&#x27;28px sans-serif&#x27;</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">fillText</span>(<span class="string">&quot;目标&quot;</span>, x, y)</span><br><span class="line"></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">strokeStyle</span> = <span class="string">&quot;#eeeeee&quot;</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">lineWidth</span> = <span class="number">1</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">beginPath</span>()</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">lineCap</span> = <span class="string">&#x27;round&#x27;</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">moveTo</span>(x + <span class="title function_">vp2px</span>(<span class="number">5</span>), y)</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">lineTo</span>(<span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">width</span> - x + <span class="title function_">vp2px</span>(<span class="number">5</span>) - <span class="variable language_">this</span>.<span class="property">marginRight</span>, y)</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">stroke</span>()</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">//绘制柱状图</span></span><br><span class="line">  <span class="title function_">drawBar</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> allWidth = <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">width</span> - <span class="variable language_">this</span>.<span class="property">barMargin</span></span><br><span class="line">    <span class="keyword">let</span> padding = (allWidth-<span class="variable language_">this</span>.<span class="property">barWidth</span>*<span class="variable language_">this</span>.<span class="property">barNum</span>)/(<span class="variable language_">this</span>.<span class="property">barNum</span>-<span class="number">1</span>)</span><br><span class="line">    <span class="keyword">let</span> startX = <span class="variable language_">this</span>.<span class="property">barMargin</span> + padding/<span class="number">2</span> <span class="comment">//第一个bar的x坐标</span></span><br><span class="line">    <span class="keyword">let</span> startY = <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">height</span> - <span class="variable language_">this</span>.<span class="property">marginBottom</span> - <span class="title function_">vp2px</span>(<span class="number">5</span>) <span class="comment">//所有bar的底点位置坐标   画布高度-底边间距-文字高度</span></span><br><span class="line">    <span class="keyword">let</span> allHeight = <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">height</span>-<span class="variable language_">this</span>.<span class="property">marginTop</span> - <span class="variable language_">this</span>.<span class="property">marginBottom</span> - <span class="title function_">vp2px</span>(<span class="number">10</span>)</span><br><span class="line">    <span class="keyword">let</span> unitValue = allHeight/<span class="variable language_">this</span>.<span class="property">targetNum</span></span><br><span class="line">    <span class="comment">// 设置线条渐变颜色</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">let</span> color = <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">createLinearGradient</span>(<span class="number">10</span>, <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">height</span>, <span class="number">10</span>, <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">height</span> - <span class="number">150</span>)</span><br><span class="line">    color.<span class="title function_">addColorStop</span>(<span class="number">0.0</span>, <span class="string">&quot;#9BE15D&quot;</span>)</span><br><span class="line">    color.<span class="title function_">addColorStop</span>(<span class="number">1.0</span>, <span class="string">&quot;#00E3AE&quot;</span>)</span><br><span class="line">    <span class="comment">// 设置线条宽度</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">lineWidth</span> =<span class="variable language_">this</span>. barWidth</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> index = <span class="number">0</span>; index &lt; <span class="variable language_">this</span>.<span class="property">items</span>.<span class="property">length</span>; index++) &#123;</span><br><span class="line">      <span class="comment">// 计算绘制的坐标位置</span></span><br><span class="line">      <span class="keyword">let</span> x = startX + (index * padding)</span><br><span class="line">      <span class="keyword">let</span> y = unitValue *<span class="variable language_">this</span>.<span class="property">items</span>[index].<span class="property">value</span>&gt;allHeight?<span class="attr">allHeight</span>:unitValue *<span class="variable language_">this</span>.<span class="property">items</span>[index].<span class="property">value</span></span><br><span class="line">      <span class="comment">//绘制每一个柱状图</span></span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">beginPath</span>()</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">lineCap</span> = <span class="string">&#x27;round&#x27;</span></span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">strokeStyle</span> = <span class="variable language_">this</span>.<span class="property">items</span>[index].<span class="property">value</span>&lt;<span class="number">100</span>?<span class="string">&quot;#e6e6e6&quot;</span>:color</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">moveTo</span>(x, startY)</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">lineTo</span>(x, startY - y)</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">stroke</span>()</span><br><span class="line">      <span class="comment">//绘制每一个柱状图对应的标签文本</span></span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">beginPath</span>()</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">textBaseline</span> = <span class="string">&quot;middle&quot;</span></span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">textAlign</span> = <span class="string">&quot;center&quot;</span></span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">fillStyle</span> = <span class="string">&quot;#999999&quot;</span></span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">font</span> = <span class="string">&#x27;28px sans-serif&#x27;</span></span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">fillText</span>(<span class="variable language_">this</span>.<span class="property">items</span>[index].<span class="property">lable</span>, x,<span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">height</span> - <span class="variable language_">this</span>.<span class="property">marginBottom</span> )</span><br><span class="line">    <span class="comment">//   绘制每一个柱状图对应的value值</span></span><br><span class="line">      <span class="keyword">if</span>(<span class="variable language_">this</span>.<span class="property">showValue</span>)&#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">beginPath</span>()</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">textBaseline</span> = <span class="string">&quot;middle&quot;</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">textAlign</span> = <span class="string">&quot;center&quot;</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">fillStyle</span> = <span class="string">&quot;#999999&quot;</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="property">font</span> = <span class="string">&#x27;28px sans-serif&#x27;</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">paint</span>.<span class="title function_">fillText</span>(<span class="variable language_">this</span>.<span class="property">items</span>[index].<span class="property">value</span>.<span class="title function_">toString</span>(), x, startY - y-<span class="title function_">vp2px</span>(<span class="number">4</span>))</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="title function_">build</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="title class_">Column</span>() &#123;</span><br><span class="line">        <span class="title class_">Canvas</span>(<span class="variable language_">this</span>.<span class="property">paint</span>)</span><br><span class="line">          .<span class="title function_">width</span>(<span class="string">&#x27;90%&#x27;</span>)</span><br><span class="line">          .<span class="title function_">height</span>(<span class="number">180</span>)</span><br><span class="line">          .<span class="title function_">margin</span>(<span class="number">30</span>)</span><br><span class="line">          .<span class="title function_">borderRadius</span>(<span class="number">20</span>)</span><br><span class="line">          .<span class="title function_">onReady</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="title function_">targetLine</span>()</span><br><span class="line">            <span class="variable language_">this</span>.<span class="title function_">drawBar</span>()</span><br><span class="line">          &#125;)</span><br><span class="line">          .<span class="title function_">shadow</span>(&#123; <span class="attr">radius</span>: <span class="number">40</span>, <span class="attr">color</span>: <span class="string">&quot;#e6e6e6&quot;</span>, <span class="attr">offsetX</span>: <span class="number">0</span>, <span class="attr">offsetY</span>: <span class="number">0</span> &#125;)</span><br><span class="line">      &#125;</span><br><span class="line">    .<span class="title function_">width</span>(<span class="string">&#x27;100%&#x27;</span>)</span><br><span class="line">    .<span class="title function_">height</span>(<span class="string">&#x27;100%&#x27;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://developer.harmonyos.com/cn/docs/documentation/doc-references-V3/ts-canvasrenderingcontext2d-0000001478181441-V3">canvasRenderingContext</a></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://houyudong1989.gitee.io/blog">侯玉东</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://houyudong1989.gitee.io/blog/2023/12/02/harmonyOS/Canvas%E7%BB%98%E5%88%B6%E6%9F%B1%E7%8A%B6%E5%9B%BE/">https://houyudong1989.gitee.io/blog/2023/12/02/harmonyOS/Canvas%E7%BB%98%E5%88%B6%E6%9F%B1%E7%8A%B6%E5%9B%BE/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://houyudong1989.gitee.io/blog" target="_blank">East'blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/harmonyOS/">harmonyOS</a></div><div class="post_share"><div class="social-share" data-image="/blog/img/library1.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2023/12/04/harmonyOS/%E5%B8%B8%E7%94%A8%E7%9F%A5%E8%AF%86%E7%82%B9/" title="常用知识点"><img class="cover" src="/blog/img/library1.jpeg" onerror="onerror=null;src='/blog/img/friend_404.gif'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">常用知识点</div></div></a></div><div class="next-post pull-right"><a href="/blog/2023/11/29/harmonyOS/want%E6%98%BE%E5%BC%8F%E9%9A%90%E5%BC%8F%E8%A7%84%E5%88%99/" title="want显式隐式规则"><img class="cover" src="/blog/img/library1.jpeg" onerror="onerror=null;src='/blog/img/friend_404.gif'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">want显式隐式规则</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/blog/2024/01/20/harmonyOS/ArkUI-X/" title="ArkUI-X"><img class="cover" src="/blog/img/library1.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-01-20</div><div class="title">ArkUI-X</div></div></a></div><div><a href="/blog/2023/12/15/harmonyOS/Canvas%E7%BB%98%E5%88%B6%E6%8A%98%E7%BA%BF%E5%9B%BE/" title="canvas绘制折线图"><img class="cover" src="/blog/img/library1.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-12-15</div><div class="title">canvas绘制折线图</div></div></a></div><div><a href="/blog/2024/03/25/harmonyOS/commonevent%E3%80%81eventhub%E3%80%81emitter%E5%92%8Cworker%E4%BD%BF%E7%94%A8/" title="commonevent、eventhub、emitter和worker使用"><img class="cover" src="/blog/img/library1.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-03-25</div><div class="title">commonevent、eventhub、emitter和worker使用</div></div></a></div><div><a href="/blog/2023/11/05/harmonyOS/hdc/" title="hdc 使用帮助"><img class="cover" src="/blog/img/library1.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-11-05</div><div class="title">hdc 使用帮助</div></div></a></div><div><a href="/blog/2023/12/26/harmonyOS/router%E8%B7%AF%E7%94%B1/" title="router路由跳转"><img class="cover" src="/blog/img/library1.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-12-26</div><div class="title">router路由跳转</div></div></a></div><div><a href="/blog/2024/03/21/harmonyOS/stage%E6%A8%A1%E5%9E%8B/" title="stage模型"><img class="cover" src="/blog/img/library1.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-03-21</div><div class="title">stage模型</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/blog/img/head.jpg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">侯玉东</div><div class="author-info__description">生命不息，奋斗不止</div></div><div class="card-info-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">70</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">26</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/houyudong1989"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/houyudong1989" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:420098321@qq.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">欢迎来到我的博客！！！</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%A6%82%E8%BF%B0"><span class="toc-number">1.</span> <span class="toc-text">概述</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Canvas"><span class="toc-number">2.</span> <span class="toc-text">Canvas</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#CanvasRenderingContext2D-%E5%92%8C-OffscreenCanvasRenderingContext2D"><span class="toc-number">3.</span> <span class="toc-text">CanvasRenderingContext2D 和 OffscreenCanvasRenderingContext2D</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8CanvasRenderingContext2D%E8%BF%9B%E8%A1%8C%E7%BB%98%E5%88%B6"><span class="toc-number">4.</span> <span class="toc-text">使用CanvasRenderingContext2D进行绘制</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8OffscreenCanvasRenderingContext2D%E8%BF%9B%E8%A1%8C%E7%BB%98%E5%88%B6"><span class="toc-number">5.</span> <span class="toc-text">使用OffscreenCanvasRenderingContext2D进行绘制</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0%E8%AF%B4%E6%98%8E"><span class="toc-number">6.</span> <span class="toc-text">常用函数说明</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#CanvasGradient-%E9%A2%9C%E8%89%B2%E6%B8%90%E5%8F%98"><span class="toc-number">7.</span> <span class="toc-text">CanvasGradient 颜色渐变</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BB%98%E5%88%B6%E6%9F%B1%E7%8A%B6%E5%9B%BE"><span class="toc-number">8.</span> <span class="toc-text">绘制柱状图</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/05/06/c++/5%E3%80%81%E5%AD%97%E7%AC%A6%E5%92%8C%E5%AD%97%E7%AC%A6%E4%B8%B2/" title="字符和字符串"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="字符和字符串"/></a><div class="content"><a class="title" href="/blog/2024/05/06/c++/5%E3%80%81%E5%AD%97%E7%AC%A6%E5%92%8C%E5%AD%97%E7%AC%A6%E4%B8%B2/" title="字符和字符串">字符和字符串</a><time datetime="2024-05-06T11:15:27.280Z" title="发表于 2024-05-06 19:15:27">2024-05-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/05/06/c++/4%E3%80%81%E6%8C%87%E9%92%88%E5%92%8C%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88/" title="指针和函数指针"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="指针和函数指针"/></a><div class="content"><a class="title" href="/blog/2024/05/06/c++/4%E3%80%81%E6%8C%87%E9%92%88%E5%92%8C%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88/" title="指针和函数指针">指针和函数指针</a><time datetime="2024-05-06T11:14:29.047Z" title="发表于 2024-05-06 19:14:29">2024-05-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/05/05/python/7%E3%80%81m3u8%E8%A7%86%E9%A2%91%E6%A0%BC%E5%BC%8F/" title="m3u8格式"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="m3u8格式"/></a><div class="content"><a class="title" href="/blog/2024/05/05/python/7%E3%80%81m3u8%E8%A7%86%E9%A2%91%E6%A0%BC%E5%BC%8F/" title="m3u8格式">m3u8格式</a><time datetime="2024-05-05T01:43:26.865Z" title="发表于 2024-05-05 09:43:26">2024-05-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/04/30/android/charles%E5%AE%89%E5%8D%93%E6%89%8B%E6%9C%BA%E6%8A%93%E5%8C%85/" title="charles 抓包"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="charles 抓包"/></a><div class="content"><a class="title" href="/blog/2024/04/30/android/charles%E5%AE%89%E5%8D%93%E6%89%8B%E6%9C%BA%E6%8A%93%E5%8C%85/" title="charles 抓包">charles 抓包</a><time datetime="2024-04-30T06:49:29.279Z" title="发表于 2024-04-30 14:49:29">2024-04-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/04/26/python/13%E3%80%81playwright/" title="playwright"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="playwright"/></a><div class="content"><a class="title" href="/blog/2024/04/26/python/13%E3%80%81playwright/" title="playwright">playwright</a><time datetime="2024-04-26T07:30:36.069Z" title="发表于 2024-04-26 15:30:36">2024-04-26</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2024 By 侯玉东</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"></div><div class="aplayer no-destroy" data-id="8873950678" data-server="netease" data-type="playlist" data-fixed="true" data-autoplay="true"> </div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-fluttering-ribbon.min.js"></script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-heart.min.js" async="async" mobile="false"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener
  btf.removeGlobalFnEvent('pjax')
  btf.removeGlobalFnEvent('themeChange')

  document.getElementById('rightside').classList.remove('rightside-show')
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

  typeof disqusjs === 'object' && disqusjs.destroy()
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()
})

document.addEventListener('pjax:error', e => {
  if (e.request.status === 404) {
    pjax.loadUrl('/blog/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>